package com.unbox.common.response;

import com.unbox.common.enums.CodeMsg;
import com.unbox.common.enums.CodeState;

/**
 * 接口返回参数
 * @param <T>
 */
public class UnboxResponse<T> {
    T body;
    int code = CodeState.OK.getValue();
    String msg;

    public UnboxResponse(){}

    public UnboxResponse(T body) {
        this.body = body;
    }

    /**
     * 验证状态
     * @return
     */
    public Boolean check() {
        if (this.code == CodeState.OK.getValue()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 验证状态如果服务调用失败直接抛出异常
     * @return
     * @throws Exception
     */
    public Boolean checkCodeThrowExcetion() throws Exception {
        if (this.code != CodeState.OK.getValue()) {
            throw new Exception(CodeMsg.SERVICE_ERROR.getValue());
        }
        return true;
    }

    UnboxResponse(Builder<T> builder) {
        this.body = builder.getBody();
        this.code = builder.getCode();
        this.msg = builder.getMsg();
    }

    /**
     * 创建一个建造者
     * @param <T>
     */
    public static class Builder<T> {
        T body;
        int code = CodeState.OK.getValue();
        String msg;

        public String getMsg() {
            return msg;
        }

        public Builder<T> setMsg(String msg) {
            this.msg = msg;
            return this;
        }

        public T getBody() {
            return body;
        }

        public Builder<T> setBody(T body) {
            this.body = body;
            return this;
        }

        public int getCode() {
            return code;
        }

        public Builder<T> setCode(int code) {
            this.code = code;
            return this;
        }

        /**
         * 忙碌返回
         * @param msg
         * @return
         */
        public Builder<T> beBusyFail(String msg) {
            this.code = CodeState.SERVICE_BE_BUSY.getValue();
            this.msg = msg;
            return this;
        }

        /**
         * 忙碌返回
         * @return
         */
        public Builder<T> beBusyFail() {
            return beBusyFail(CodeMsg.SERVICE_BE_BUSY.getValue());
        }

        /**
         * 业务错误返回
         * @param msg
         * @return
         */
        public Builder<T> businessFail(String msg) {
            this.code = CodeState.BUSINESS_ERROR.getValue();
            this.msg = msg;
            return this;
        }

        /**
         * 业务错误返回
         * @return
         */
        public Builder<T> businessFail() {
            return businessFail(CodeMsg.BUSINESS_ERROR.getValue());
        }

        /**
         * 服务调用返回
         * @param msg
         * @return
         */
        public Builder<T> serviceFail(String msg) {
            this.code = CodeState.SERVICE_ERROR.getValue();
            this.msg = msg;
            return this;
        }

        /**
         * 服务异常返回
         * @return
         */
        public Builder<T> serviceFail() {
            return serviceFail(CodeMsg.SERVICE_ERROR.getValue());
        }

        /**
         * 成功返回
         * @return
         */
        public Builder<T> success() {
            this.code = CodeState.OK.getValue();
            return this;
        }

        /**
         * 成功返回
         * @param body
         * @param msg
         * @return
         */
        public Builder<T> success(T body, String msg) {
            this.code = CodeState.OK.getValue();
            this.body = body;
            this.msg = msg;
            return this;
        }

        /**
         * 成功返回
         * @param body
         * @param msg
         * @param pageNo
         * @param pageSize
         * @param pageCount
         * @param sumDataCount
         * @return
         */
        public Builder<T> success(T body, String msg, int pageNo, int pageSize, int pageCount, int sumDataCount) {
            this.code = CodeState.OK.getValue();
            this.body = body;
            this.msg = msg;
            return this;
        }

        public UnboxResponse<T> build() {
            return new UnboxResponse<T>(this);
        }
    }


    public T getBody() {
        return body;
    }

    public void setBody(T body) {
        this.body = body;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}
